#ifndef SPRITEX_H
#define SPRITEX_H

#include <iostream>
#include <vector>
#include <fstream>
#include <string>
#include <sstream>

using std::string;
using std::cerr;
using std::endl;
using std::ifstream;
using std::ios_base;
using std::vector;

struct tile_t {
    short x,y,w,h;
};

struct frame_hdr_t {
    short length;
    short tileCount;
    short collisionCount;
    short referCount;
    short top;
    short buttom;
    short left;
    short right;
};

struct frame_data_tile_t {
    short index,x,y,transform;
};

struct frame_data_collision_t {
    short x,y,w,h;
};

struct frame_data_refer {
    short x,y;
};

struct frame_data_t {
    vector<frame_data_tile_t> tile;
    vector<frame_data_collision_t> collision;
    vector<frame_data_refer> refer;
};

struct frame_t {
    frame_hdr_t hdr;
    frame_data_t data;
};

struct action_hdr_t {
    short length;
    short sequencecount;
    short hasdelay;
    short transform;
};

struct action_data_seq_t {
    short sequence;
    short delaytime;
};

struct action_data_t {
    vector<action_data_seq_t> seq;
};

struct action_t {
    action_hdr_t hdr;
    action_data_t data;
};

class SpriteX
{
public:
    SpriteX(const string&);
    ~SpriteX();
    bool loadSpx(const string&);

protected:
    void clear();
    bool getTile(ifstream&in,tile_t&t);
    bool getFrame(ifstream&in,frame_t&f);
    bool getAction(ifstream&in,action_t&a);

private:
    int header;
    int version;
    int byteorder;
public:
    int tileCount;
    int frameCount;
    int actionCount;
    vector<tile_t> tileList;
    vector<frame_t> frameList;
    vector<action_t> actionList;
};


#endif // SPRITEX_H
